<?php
function insereResposta($conn){
	
$resposta = trim($_POST["resposta"]);
$resposta = $conn->real_escape_string($resposta);
$idEquipa = trim($_SESSION["id"]);

$query = "SELECT id FROM jornadas_tbl WHERE data_ini <= NOW() AND data_fim >= NOW()";
$result=$conn->query($query);
echo $conn->error;

if($result->num_rows != 0){
		$linha = $result->fetch_object();
		$jornadaID
		 = $linha->id;
}
$query = "INSERT INTO `respostas_tbl`(`id_jornada`, `id_equipa`, `resposta`, `data_criacao`)
VALUES ($jornadaID,$idEquipa,'".iconv("utf-8", "windows-1252",$resposta)."',NOW())";

$result=$conn->query($query);
echo $conn->error;

if (!$result)
	header("location: ../publico/insertion_fail.php");
header("location: ../publico/insertion_success.php");
}

// UPDATE
function updateResposta($conn,$resID){
	
$resposta = trim($_POST["resposta"]);
$resposta = $conn->real_escape_string($resposta);
$idEquipa = trim($_SESSION["id"]);

$query = "UPDATE respostas_tbl SET resposta='".iconv("utf-8", "windows-1252", $resposta)."',
			data_mod=NOW()
			WHERE id=".$resID;

$result=$conn->query($query);
echo $conn->error;

if (!$result)
	header("location: ../publico/insertion_fail.php");
header("location: ../publico/insertion_success.php");
}

function getResposta($conn){
	$query = "SELECT `id`, `resposta`
	FROM `respostas_tbl` 
	WHERE `id_equipa` = ".$_SESSION["id"]." AND `id_jornada` =
	(SELECT id FROM jornadas_tbl WHERE data_ini <= NOW() AND data_fim >= NOW())";
	
	$result=$conn->query($query);
	echo $conn->error;
	
	if($result->num_rows != 0){
		$resposta = array();
		$linha = $result->fetch_object();
		$resposta["id"] = $linha->id;
		$resposta["resposta"] = $linha->resposta;
	
		return $resposta;

	}
}
?>